package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.InvitationMgrRemote;
import it.polimi.travelDream.ejb.interfaces.PackageMgrRemote;
import it.polimi.travelDream.entities.Invitation;
import it.polimi.travelDream.entities.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Map;
import java.util.Properties;

import javax.ejb.EJB;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;




/**
 * Servlet implementation class MailServlet
 */
@WebServlet("/MailServlet2")
public class MailServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
	@EJB
	InvitationMgrRemote imgr;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MailServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		        Map<String, String[]> mappa = request.getParameterMap();
		        
	 
				final String username = "traveldreamsystem@gmail.com";
				final String password = "nicetry89";
		 
				Properties props = new Properties();
				props.put("mail.smtp.auth", "true");
				props.put("mail.smtp.starttls.enable", "true");
				props.put("mail.smtp.host", "smtp.gmail.com");
				props.put("mail.smtp.port", "587");
		 
				Session session = Session.getInstance(props,
				  new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				  });
		 
				try {
		 
					Message message = new MimeMessage(session);
					message.setFrom(new InternetAddress("traveldreamsystem@gmail.com"));
					message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(mappa.get("to")[0]));
					message.setSubject("Invito to TravelDream");
					message.setText(mappa.get("message")[0]);
		 
					Transport.send(message);
		 
					System.out.println("Done");
					
					User z=(User)request.getSession().getAttribute("utente");
					
					
					Invitation invitation = new Invitation(1, z.getId(), mappa.get("to")[0], 0);
					imgr.addInvitation(invitation);
					
					
					
					request.getRequestDispatcher("/UHome.jsp").forward(request, response);
				} catch (MessagingException e) {
					throw new RuntimeException(e);
				}
			}
		
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}


